/*
 * main.cpp
 *
 *  Created on: 10/09/2014
 *      Author: Samuel
 */

#include "main.h"

int main (int argc, char **argv) {
     try {
	     AppendWriter writer("result/result.txt");
	     writer.open();
	     writer.appendLine("\t\t\t Valor F1 \t Tempo F1 \t Valor F2 \t Tempo F2 \t Valor F3 \t Tempo F3");
		 for(int j=0; j<10;j++){
		 	writer.append("Instancia" << j << "\t");
		    for(int i=1; i<4;i++){	    
	    	 	solveProblem(i,j, &outFile);
		    }
		    writer.endLine();
		 }
		 writer.close();
     }catch (IloException& e) {
        cerr << "Concert exception caught: " << e << endl;
     }catch (...) {
        cerr << "Unknown exception caught" << endl;
     }
     return 0;
}

void main::nomearVariaveis(IloNumVarArray var1, IloNumVarArray var2){
  	for(int i=0;i<4;i++){
  		var1[i].setName(string("X(" + intToString(i) + ")").c_str());
  		var2[i].setName(string("Y(" + intToString(i) + ")").c_str());
  	}
}
  
string main::intToString(int number){
  	stringstream ss;
    ss << number;
    return ss.str();
}  
  
void main::solveProblem(int formType, int inst, ofstream* out){
	AdjacencyMatrix adjMat (string("instancia/matrix" + intToString(inst) + ".txt").c_str());
	Solver form;
	IloEnv   env;
	IloModel model(env);
	IloRangeArray con(env);
	IloNumVarArray var1(env, adjMat.size(), 0.0, 1.0, ILOFLOAT);
    IloNumVarArray var2(env, adjMat.size(), 0.0, 1.0, ILOFLOAT);
    nomearVariaveis(var1, var2);
  	switch(formType){
  		case 1:
  			form.formulation1(model, var1, var2, con, adjMat);
  		break;
  		case 2:
  			form.formulation2(model, var1, var2, con, adjMat);
  			break;
  		case 3:
  			form.formulation3(model, var1, var2, con, adjMat);
  		break;
  	}
  	con.end();
  	var1.end();
   	var2.end();
   	
  	IloCplex cplex(model);
  	if ( !cplex.solve() ) {
           env.error() << "Failed to optimize LP" << endl;
           throw(-1);
    }
    //outFile << "Status = " << cplex.getStatus() << endl;
    (*out) << cplex.getObjValue() << "\t" << cplex.getTime() << "\t";

	cplex.exportModel(string("model/modelForm" + intToString(formType) + "Inst"+ intToString(inst) + ".lp").c_str());
	env.end();
}
    
main::main() {
	// TODO Auto-generated constructor stub

}

main::~main() {
	// TODO Auto-generated destructor stub
}

